//
//  TTSpecialViewController.m
//  TaoTuo
//
//  Created by LG on 2020/10/19.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTSpecialViewController.h"
#import "TTSpecialViewCell.h"
#import "TTActivityModel.h"
#import "TTSpecialItemViewController.h"

static NSString *cellID = @"cell";

@interface TTSpecialViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UIImage *shadowImage;

@end

@interface TTSpecialViewController ()

@end

@implementation TTSpecialViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.shadowImage = self.navigationController.navigationBar.shadowImage;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.shadowImage = self.shadowImage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"专题";
    
    self.page = 1;
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self getActivityListDatas];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page++;
        [self getActivityListDatas];
    }];
    
    [self getActivityListDatas];
    
}

/// 获取专题活动列表
- (void)getActivityListDatas
{
    NSDictionary *dic = @{
        @"param.pageIndex" : @(self.page),
        @"param.pageSize" : @20
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:activityList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        NSArray *array = responseObject;
        for (NSDictionary *dic in array) {
            TTActivityModel *model = [TTActivityModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        
        [self.tableView.mj_header endRefreshing];
        if (array.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTSpecialViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTActivityModel *model = self.dataArray[indexPath.row];
    TTSpecialItemViewController *itemVC = [[TTSpecialItemViewController alloc] initWithActivityId:model.Id];
    [self.navigationController pushViewController:itemVC animated:YES];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 250 * kScale;
        _tableView.emptyImageName = @"tao_nodata";
        [_tableView registerClass:[TTSpecialViewCell class] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
